import React, {Component} from 'react';
import './index.less';
import {base, high} from 'nc-lightapp-front';
import Header from "../components/Header";
import LeftTree from '../components/LeftTree';
import SearchModal from "../components/SearchModal";
import getMainTableData from './functions/getMainTableData';
import MainTable from "../components/MainTable";
import SortModal from "../components/SortModal";
import toggleModal from "./functions/toggleModalVisible";
import updateButtonStatus from "./functions/updateButtonStatus";
import SubTable from "../components/SubTable";
import AddNew from "../components/AddNew";
import BatchEditModal from "../components/BatchEditModal";
import clearMainTableData from "./functions/clearMainTableData";
import {getAppPageConfig, handleHash} from "src/hrpub/common/utils/utils";
import getSysConf from "./functions/getSysConf";

@handleHash('20198162232323', '/ifr?page=20198162232323&c=60072060&p=60072060p&ar=0001Z700APPN60072060')

class PartTimeRecord extends Component {
    constructor(props) {
        super(props);
        if (props.use) {
            props.use.form('parttime', 'partadd');
            props.use.table('info');
            props.use.editTable('hi_psndoc_part');
            props.use.search('query');
        }
        this.config = getAppPageConfig();
        this.state = {
            orgVal: {},
            json: {},
            queryModel: '1',
            searchModalValue: {},
            queryOid: '',
            pageInfo: {
                pageSize: 10,
                pageIndex: 1,
                total: 0,
                totalPage: 1
            },
            sortModalVisible: false,
            hasSearched: false,
            addNewModalVisible: false,
            batchEditModalVisible: false,
            isIncHisPartJob: false, //在职人员
            currentSelectTreeNode: '',
            currentSelectTreeNode2: '',
            selectedPsn: '',
            selectedPsnPk: '',
            disabledAll: false,
            focusedRow: ''
        };
        props.createUIDom(this.config, (data) => {
            this.setState({
                buttons: data.button || [],
                context: data.context || {},
                queryOid: data.template.query && data.template.query.oid,
                pageid: data.template.pageid
            });

            //data.template = processTemplate.call(this, data.template);

            props.meta.setMeta(data && data.template ? data.template : {});
            props.button.setButtons(data && data.button ? data.button : {});
            updateButtonStatus.call(this);
            props.button.setButtonDisabled({
                'delete': true,
                'edit': true,
                'change': true
            })
            this.subTableNode.addAction();
        });
        props.MultiInit.getMultiLang({
            moduleId: 'hi6007', domainName: 'hrhi', callback: (json, status, inlt) => {
                if (status) {
                    this.setState({json, inlt})//存json和inlt到页面state中并刷新页面
                }
            }
        });
        this.updateState = this.updateState.bind(this);
        this.getMainTableData = this.getMainTableData.bind(this);
        this.toggleModal = this.toggleModal.bind(this);
        this.sortModalOnSure = this.sortModalOnSure.bind(this);
        this.selectTree = this.selectTree.bind(this);
        this.selectTree2 = this.selectTree2.bind(this);
        this.updateOrgValue = this.updateOrgValue.bind(this);
        this.updateQueryModel = this.updateQueryModel.bind(this);
        this.disabledAllArea = this.disabledAllArea.bind(this);
        this.addNew = this.addNew.bind(this);
    }

    componentDidMount() {
        if (window.location.href.match('localhost:3006')) window.location.hash = `#/ifr?page=20198162232323`;
    }

    updateOrgValue(value) {

        this.setState({
            orgVal: value,
            currentSelectTreeNode: '',
            currentSelectTreeNode2: ''
        }, async () => {
            await this.getSysConf(value);
            if (this.state.hasSearched) {
                if (!value.refpk) {
                    clearMainTableData.call(this);
                } else {
                    this.getMainTableData();
                }
            } else {
                updateButtonStatus.call(this);
            }
        })
    }

    getSysConf = (orgValue) => {
        return getSysConf.call(this, orgValue);
    };

    updateQueryModel(value) {
        this.setState({
            queryModel: value
        }, () => {
            if (this.state.hasSearched) {
                this.getMainTableData()
            } else {
                updateButtonStatus.call(this);
            }
        })
    }

    updateState(data, callback = () => {
    }) {
        this.setState(data, callback)
    }

    getMainTableData(isAfterBatch) {
        getMainTableData.call(this, isAfterBatch);
    }

    // 切换各种弹窗显示隐藏，type为弹窗的显示隐藏state的key值
    toggleModal(type, visible) {
        return toggleModal.call(this, type, visible);
    }

    // 排序弹窗点击确定的回调
    sortModalOnSure() {
        const {pageInfo, hasSearched} = this.state;
        if (hasSearched) {
            this.setState({
                pageInfo: {
                    ...pageInfo,
                    pageIndex: 1
                }
            }, () => {
                this.getMainTableData();
            });
        }
    }

    //左侧树节点选择回调
    selectTree(pk) {
        const {pageInfo, hasSearched} = this.state;
        this.setState({
            currentSelectTreeNode: pk,
            pageInfo: {
                ...pageInfo,
                pageIndex: 1,
                total: 0,
                totalPage: 1
            }
        }, () => {
            if (hasSearched) {
                this.getMainTableData();
            }
        });
    }

    selectTree2(pk) {
        const {pageInfo, hasSearched} = this.state;
        this.setState({
            currentSelectTreeNode2: pk,
            pageInfo: {
                ...pageInfo,
                pageIndex: 1,
                total: 0,
                totalPage: 1
            }
        }, () => {
            if (hasSearched) {
                this.getMainTableData();
            }
        });
    }

    disabledAllArea(disabledAll) {
        this.setState({
            disabledAll
        }, () => {
            updateButtonStatus.call(this);
        });
        this.props.syncTree.setNodeDisable('departmentTree', disabledAll);
        this.props.syncTree.setNodeDisable('departmentTree2', disabledAll);
    }

    addNew() {
        const {pageInfo, hasSearched} = this.state;
        if (hasSearched) {
            this.setState({
                pageInfo: {
                    ...pageInfo,
                    pageIndex: 1
                }
            }, () => {
                this.getMainTableData(true);
            });
        }
    }

    render() {
        const {
            orgVal, queryOid, pageInfo, sortModalVisible, addNewModalVisible, json, pageid,
            batchEditModalVisible, isIncHisPartJob, hasSearched, selectedPsn, disabledAll, queryModel,
            currentSelectTreeNode, currentSelectTreeNode2, focusedRow, buttons
        } = this.state;
        return (
            <div className='entry-staff-query nc-bill-card'>
                <Header {...this.props}
                        json={json}
                        getMainTableData={this.getMainTableData}
                        orgVal={orgVal}
                        hasSearched={hasSearched}
                        selectedPsn={selectedPsn}
                        pageInfo={pageInfo}
                        isIncHisPartJob={isIncHisPartJob}
                        queryModel={queryModel}
                        disabledAll={disabledAll}
                        updateState={this.updateState}
                        updateOrgValue={this.updateOrgValue}
                        updateQueryModel={this.updateQueryModel}/>
                <div className="entry-staff-query-content">
                    <div className="left-content">
                        <div style={{display: queryModel === '1' ? 'block' : 'none'}}>
                            <LeftTree
                                {...this.props}
                                json={json}
                                queryModel={'1'}
                                onSelect={this.selectTree}
                                pk_org={orgVal.refpk}
                                treeId="departmentTree"
                                disabledAll={disabledAll}
                            />
                        </div>
                        <div style={{display: queryModel === '2' ? 'block' : 'none'}}>
                            <LeftTree
                                {...this.props}
                                json={json}
                                queryModel={'2'}
                                onSelect={this.selectTree2}
                                pk_org={orgVal.refpk}
                                treeId="departmentTree2"
                                disabledAll={disabledAll}
                            />
                        </div>
                    </div>
                    <div className="right-content">
                        <MainTable
                            {...this.props}
                            focusedRow={focusedRow}
                            json={json}
                            orgVal={orgVal}
                            updateState={this.updateState}
                            getMainTableData={this.getMainTableData}
                            pageInfo={pageInfo}
                            disabledAll={disabledAll}
                        />
                        <SubTable
                            {...this.props}
                            json={json}
                            buttons={buttons}
                            orgVal={orgVal}
                            pageid={pageid}
                            selectedPsn={selectedPsn}
                            isIncHisPartJob={isIncHisPartJob}
                            disabledAll={disabledAll}
                            queryModel={queryModel}
                            disabledAllArea={this.disabledAllArea}
                            getMainTableData={this.getMainTableData}
                            currentSelectTreeNode={currentSelectTreeNode}
                            currentSelectTreeNode2={currentSelectTreeNode2}
                            ref={node => this.subTableNode = node}
                        />
                    </div>
                </div>

                <SearchModal
                    {...this.props}
                    json={json}
                    pageInfo={pageInfo}
                    queryOid={queryOid}
                    updateState={this.updateState}
                    getMainTableData={this.getMainTableData}
                />

                <SortModal
                    {...this.props}
                    json={json}
                    visible={sortModalVisible}
                    onSure={this.sortModalOnSure}
                    onClose={this.toggleModal('sortModalVisible', false)}
                    templateId="sort"
                />

                <AddNew
                    {...this.props}
                    json={json}
                    orgVal={orgVal}
                    visible={addNewModalVisible}
                    onSure={this.addNew}
                    onClose={this.toggleModal('addNewModalVisible', false)}
                />

                <BatchEditModal
                    {...this.props}
                    json={json}
                    orgVal={orgVal}
                    isIncHisPartJob={isIncHisPartJob}
                    visible={batchEditModalVisible}
                    onClose={this.toggleModal('batchEditModalVisible', false)}
                    getMainTableData={this.getMainTableData}
                />

            </div>
        );
    }
}

export default PartTimeRecord;
